<?php
class Core_Driver_MysqlImproved implements Core_Library_Database {
	
	private $connection;
	private $query;
	private $result;
	
	public function connect($server = null, $user = null, $password = null, $database = null, $port = null, $cocket = null){
		try {
			if($this->connection = new mysqli($host, $user, $password, $database, $port, $socket)){
				return TRUE;
			}
			else {
				throw new Exception("Nie można uzyskać połączenia z bazą danych!");
			}
		}
		catch(Exception $e){
			return $e->getMessage();
		}
	}
	
	public function disconnect(){
		try {
			if($this->connection->close()){
				return TRUE;
			}
			else {
				throw new Exception("Nie można zamknąć połączenia z bazą danych!");
			}
		}
		catch(Exception $e){
			return $e->getMessage();
		}
	}
	
	public function prepare($query){
		try {
			$this->query = $query;
			if(($query != null) && ($query != "") && (trim($query) != "")){
				return $true;
			}
			else {
				throw new Exception("Musisz wpisać zapytanie SQL!");
			}
		}
		catch(Exception $e){
			return $e->getMessage();
		}
	}
	
	public function escape($data){
		try {
			if(($data != null) && (trim($data) != null)){
				return $this->connection->real_escape_string($data);
			}
			else{
				throw new Exception("Dane do filtrowania nie mogą być puste!");
			}
		}
		catch(Exception $e){
			return $e->getMessage();
		}
	}
	
	public function query(){
		try {
			if($this->connection->query($this->query)){
				$this->result = $this->connection->query($this->query);
				return TRUE;
			}
			else {
				throw new Exception("Nie udało się wykonać zapytania SQL: ".$this->query());
			}
		}
		catch(Exception $e){
			return $e->getMessage();
		}
		return FALSE;
	}
	
	public function update(){
		try {
			if(isset($this->result)){
				return $this->connection->affected_rows;
			}
			else {
				throw new Exception("Aktualizacja rekordów nie powiodła się!");
			}
		}
		catch(Exception $e){
			return $e->getMessage();
		}
		return FALSE;
	}
	
	public function insert(){
		try {
			if(isset($this->result)){
				return $this->connection->affected_rows;
			}
			else {
				throw new Exception("Nie można dodać nowych rekordów!");
			}
		}
		catch(Exception $e){
			return $e->getMessage();
		}
		return FALSE;
	}
	
	public function delete(){
		try {
			if(isset($this->result)){
				return $this->connection->affected_rows;
			}
			else {
				throw new Exception("Nie można usunąć wybranych rekordów!");
			}
		}
		catch(Exception $e){
			return $e->getMessage();
		}
		return FALSE;
	}
	
	public function fetch($type = 'object'){
		try {
			if(isset($this->reuslt)){
				switch($type){
					case 'array':
						$row = $this->result->fetch_array();
						break;
					case 'object':
						$row = $this->result->fetch_object();
						break;
					default:
						$row = $this->result->fetch_object();
						break;
				}
				return $row;
			}
			else {
				throw new Exception("Nie udało się pobrać żadnych rekordów!");
			}
		}
		catch(Exception $e){
			return $e->getMessage();
		}
	}
}